<?php

include_once('data.php');

class RSS{

        public static function generateUserRSS($user_id=NULL){
        
                $publishers=NULL;

                // gets publishers for the specified user_id
                if($user_id!==NULL){
                        try{
                                $publishers = Publisher::get_publishers_by_user_id($user_id);
                        }catch(Exception $e){
                                error_log("[nor] Someone tried to retrieve RSS info from user $user_id");
                        }
                }

                if($user_id===NULL or $publishers===NULL){
                        $publishers = Publisher::get_publishers();
                }

               $entries = Entry::get_entries_restricted_pub_limit($publishers, 0, 20 ); 
               $entries = $entries['value_extract_1'];

               $doc = RSS::generateRSSDOMTemplate();
               $root = $doc->getElementsByTagName('channel')->item(0);

               foreach($entries as $entry){
                   $root->appendChild(RSS::getEntryDOMElement($entry,$doc)); 
               }

               return $doc;
        }

        public static function getEntryDOMElement($entry, $doc){ 
                
                $pub = new Publisher($entry->publisher_id);

                $elem = $doc->CreateElement('item');
                $elem->appendChild($doc->createElement('title', $entry->title_e));
                $elem->appendChild($doc->createElement('link' , $pub->url));
                //TODO$elem->appendChild($doc->createElement('guid' , $pub->url));
                $date = date_create($entry->date);
                $elem->appendChild($doc->createElement('pubDate', $date->format(DATE_RFC822)));
                $source = $doc->createElement('source', 'Nor² RSS');
                $source->setAttribute('url', 'http://localhost/nor/rss.php');
                $elem->appendChild($source);
                $elem->appendChild($doc->createElement('description', $entry->content_e));

                return $elem;
        }

        public static function generateRSSDOMTemplate(){

               $doc = new DOMDocument('1.0', 'UTF-16');
               
               /*root RSS element*/
               $rss = $doc->createElement('rss', '');
               $rss->setAttribute('version', '2.0');
               $doc->appendChild($rss);
               
               /*Channel info*/
               $channel = $doc->createElement('channel','');
               $rss->appendChild($channel);
                
               $title = $doc->createElement('title', 'Not other RSS reader');
               $channel->appendChild($title);

               $link = $doc->createElement('link','http://localhost/nor');
               $channel->appendChild($link);

               $language = $doc->createElement('language','ES-es');
               $channel->appendChild($language);

               $copyright1 = $doc->createElement('copyright','2012, Luis A. Arce and Víctor Ramírez');
               $channel->appendChild($copyright1);

               $generator = $doc->createElement('generator','Nor²');
               $channel->appendChild($generator);

               $description = $doc->createElement('description','El agregador y lector de feeds definitivo');
               $channel->appendChild($description);

               $lastBuildDate = $doc->createElement('lastBuildDate', date(DATE_RFC822));
               $channel->appendChild($lastBuildDate);

               return $doc;
               
               }
}

header('Content-type: application/rss+xml'); 

if(isset($_GET['user'] ))
        $user = $_GET['user'];
try{
print(RSS::generateUserRSS($user)->saveXML());

}catch(Exception $e){}
?>
